home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / s_to_z / spidr100 / setup.arv / HASHTEST.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-09-15  |  6.2 KB  |  253 lines

  1. {----------------------------------------------------------------------------
  2. |
  3. | Library: Spider Containers for Object Pascal
  4. |
  5. | Module: HastTest.Pas
  6. |
  7. | Description: Form to test hash and string table classes.
  8. |              Since a TStringTable uses a THashTable for implementation,
  9. |              a TStringTable instance is used as the container for this
  10. |              test form.
  11. |
  12. | History: Version 1.0  March 1996. Copyright (c) 1996 Michel Brazeau
  13. |                                   Interval Software
  14. |
  15. |---------------------------------------------------------------------------}
  16. unit HashTest;
  17.  
  18. interface
  19.  
  20. uses
  21.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  22.   Forms, Dialogs, StdCtrls,
  23.  
  24.   StrTable;  { TStringTable }
  25.  
  26. type
  27.   THashTableForm = class(TForm)
  28.     ListBox: TListBox;
  29.     ItemCount: TLabel;
  30.     AddButton: TButton;
  31.     SearchButton: TButton;
  32.     DeleteButton: TButton;
  33.     ClearButton: TButton;
  34.     LoadButton: TButton;
  35.     procedure FormCreate(Sender: TObject);
  36.     procedure FormDestroy(Sender: TObject);
  37.     procedure AddButtonClick(Sender: TObject);
  38.     procedure FormClose(Sender: TObject; var Action: TCloseAction);
  39.     procedure ClearButtonClick(Sender: TObject);
  40.     procedure SearchButtonClick(Sender: TObject);
  41.     procedure LoadButtonClick(Sender: TObject);
  42.     procedure DeleteButtonClick(Sender: TObject);
  43.   private
  44.     StringTable : TStringTable;
  45.  
  46.     { redraws the list box, from the contents of the hash table }
  47.     procedure UpdateListBox;
  48.  
  49.     { iterator method to add a TStringCombo to the list box }
  50.     procedure AddString(const Obj : TObject);
  51.  
  52.   public
  53.  
  54.     { Public declarations }
  55.   end;
  56.  
  57. implementation
  58.  
  59. {$R *.DFM}
  60.  
  61. uses
  62.       ObjTest,    { TestForm }
  63.       ObjList,    { TUnorderedList }
  64.       ObjBuckt;   { TStringCombo }
  65.  
  66. const
  67.     { Hash table size (number of unique hash entries in table }
  68.     CHashTableSize = 500;
  69.  
  70. {--------------------------------------------------------------------------}
  71.  
  72. procedure THashTableForm.FormCreate(Sender: TObject);
  73. begin
  74.     StringTable := TStringTable.Create(CHashTableSize);
  75. end;
  76.  
  77. {--------------------------------------------------------------------------}
  78.  
  79. procedure THashTableForm.FormDestroy(Sender: TObject);
  80. begin
  81.     StringTable.Free;
  82. end;
  83.  
  84. {--------------------------------------------------------------------------}
  85.  
  86. procedure THashTableForm.UpdateListBox;
  87. begin
  88.     ListBox.Clear;
  89.  
  90.     Screen.Cursor := crHourGlass;
  91.  
  92.     ListBox.Enabled := False;
  93.  
  94.     try
  95.  
  96.         StringTable.ForEachCallMethod(AddString);
  97.  
  98.     finally
  99.  
  100.         ListBox.Enabled := True;
  101.         Screen.Cursor := crDefault;
  102.  
  103.     end;
  104.  
  105.     ItemCount.Caption := IntToStr(StringTable.Size);
  106. end;
  107.  
  108. {--------------------------------------------------------------------------}
  109.  
  110. procedure THashTableForm.AddString(const Obj : TObject);
  111. begin
  112.     ListBox.Items.Add((Obj as TStringCombo).Str);
  113. end;
  114.  
  115. {--------------------------------------------------------------------------}
  116.  
  117. procedure THashTableForm.AddButtonClick(Sender: TObject);
  118. const
  119.     Str : String = '';
  120. begin
  121.     if not InputQuery('', 'String to add: ', Str) then
  122.         Exit;
  123.  
  124.     StringTable.Insert(Str, nil);
  125.  
  126.     UpdateListBox;
  127. end;
  128.  
  129. {--------------------------------------------------------------------------}
  130.  
  131. procedure THashTableForm.FormClose( Sender: TObject;
  132.                                     var Action: TCloseAction);
  133. begin
  134.     Action := caFree;
  135. end;
  136.  
  137. {--------------------------------------------------------------------------}
  138.  
  139. procedure THashTableForm.ClearButtonClick(Sender: TObject);
  140. begin
  141.     StringTable.Clear;
  142.  
  143.     UpdateListBox;
  144. end;
  145.  
  146. {--------------------------------------------------------------------------}
  147.  
  148. procedure THashTableForm.SearchButtonClick(Sender: TObject);
  149. const
  150.     Str       : String   = '';
  151.     OccurStr  : String   = '1';
  152.  
  153. var
  154.     Occur     : LongInt;
  155.  
  156. begin
  157.     if not InputQuery('', 'Search for : ', Str) then
  158.         Exit;
  159.  
  160.     if not InputQuery('', 'Occurence : ', OccurStr) then
  161.         Exit;
  162.  
  163.     Occur := StrtoInt(OccurStr);
  164.  
  165.     if StringTable.Search(Str, Occur) <> nil then
  166.         MessageDlg('String found', mtInformation,[mbOk], 0)
  167.     else
  168.         MessageDlg('String NOT found!', mtInformation,[mbOk], 0);
  169. end;
  170.  
  171. {--------------------------------------------------------------------------}
  172.  
  173. procedure THashTableForm.LoadButtonClick(Sender: TObject);
  174. var
  175.     StringList   : TUnorderedList;
  176.  
  177.     Str          : String;
  178.  
  179.     I            : LongInt;
  180.  
  181. begin
  182.     StringList := TUnOrderedList.Create(TStringCombo, CompareStringCombo);
  183.     try
  184.         ListBox.Enabled := False;
  185.  
  186.         TestForm.LoadStringsFromFile(StringList);
  187.  
  188.         Screen.Cursor := crHourGlass;
  189.  
  190.         try
  191.             I := 1;
  192.  
  193.             { insert all the values in StringList }
  194.             if StringList.GotoFirst then
  195.             repeat
  196.                 { give other applications processing time }
  197.                 if (I mod 500) = 0 then
  198.                     Application.ProcessMessages;
  199.                 Inc(I);
  200.  
  201.                 Str := (StringList.CurrentObj as TStringCombo).Str;
  202.  
  203.                 StringTable.Insert(Str, nil);
  204.  
  205.            until not StringList.GotoNext;
  206.  
  207.         finally
  208.             Screen.Cursor := crDefault;
  209.  
  210.             ListBox.Enabled := True;
  211.         end;
  212.  
  213.     finally
  214.         StringList.Free;
  215.  
  216.         ListBox.ItemIndex := ListBox.Items.Count - 1;
  217.  
  218.         UpdateListBox;
  219.     end;
  220. end;
  221.  
  222. {--------------------------------------------------------------------------}
  223.  
  224. procedure THashTableForm.DeleteButtonClick(Sender: TObject);
  225. const
  226.     Str       : String  = '';
  227.     OccurStr  : String  = '1';
  228.  
  229. var
  230.     Occur     : LongInt;
  231.  
  232. begin
  233.     if not InputQuery('', 'String to delete : ', Str) then
  234.         Exit;
  235.  
  236.     if not InputQuery('', 'Occurence : ', OccurStr) then
  237.         Exit;
  238.  
  239.     Occur := StrtoInt(OccurStr);
  240.  
  241.     if StringTable.Delete(Str, Occur) then
  242.     begin
  243.         MessageDlg('String deleted', mtInformation,[mbOk], 0);
  244.         UpdateListBox;
  245.     end
  246.     else
  247.         MessageDlg('String NOT found!', mtInformation,[mbOk], 0);
  248. end;
  249.  
  250. {--------------------------------------------------------------------------}
  251.  
  252. end.
  253.